package seismosurfer.gui.panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Iterator;
import java.util.LinkedList;

import javax.help.CSH;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import com.bbn.openmap.Layer;
import com.bbn.openmap.LayerHandler;
import com.bbn.openmap.gui.LayerPane;
import com.bbn.openmap.gui.LayersPanel;
import com.bbn.openmap.util.Debug;

/**
 * This panel acts as the legend of the map from which you
 * can control the layers and their properties.
 *
 */
public class SeismoLayersPanel extends LayersPanel {
    
    private static final long serialVersionUID = -2379271527226939288L;

    public SeismoLayersPanel() {
        super();
        CSH.setHelpIDString(this, "legend");
    }

    public SeismoLayersPanel(LayerHandler lHandler) {
        super(lHandler);
        CSH.setHelpIDString(this, "legend");
    }

    public void createPanel(Layer[] inLayers) {

        if (scrollPane != null) {
            remove(scrollPane);
        }

        Layer[] layers = inLayers;
        if (layers == null) {
            layers = new Layer[0];
        }

        if (panesPanel == null) {
            panesPanel = new JPanel();
            panesPanel.setLayout(new BoxLayout(panesPanel, BoxLayout.Y_AXIS));
            panesPanel.setAlignmentX(LEFT_ALIGNMENT);
            panesPanel.setAlignmentY(BOTTOM_ALIGNMENT);
        } else {
            ((BoxLayout) panesPanel.getLayout()).invalidateLayout(panesPanel);
            panesPanel.removeAll();
        }

        if (bg == null) {
            bg = new ButtonGroup();
        }

        LinkedList panes = new LinkedList();
        LinkedList backgroundPanes = new LinkedList();

        // populate the arrays of CheckBoxes and strings used to fill
        // the JPanel for the panes
        for (int i = 0; i < layers.length; i++) {
            Layer layer = layers[i];
            if (layer == null) {
                Debug.output("LayersPanel caught null layer, " + i + " out of "
                        + layers.length);
                continue;
            }

            LayerPane lpane = (LayerPane) paneLookUp.get(layer);

            if (lpane == null) {
                if (Debug.debugging("layercontrol")) {
                    Debug.output("LayersPanel: Creating LayerPane for "
                            + layer.getName());
                }
                lpane = createLayerPaneForLayer(layer, layerHandler, bg);
                lpane.addPropertyChangeListener(LayerSelectedCmd, this);
                lpane.addPropertyChangeListener(LayerDeselectedCmd, this);
                paneLookUp.put(layer, lpane);
            } else {
                // In case this has been modified elsewhere...
                lpane.setLayerOn(layer.isVisible());
            }

            if (layer.getAddAsBackground() && backgroundLayerSeparator != null) {
                backgroundPanes.add(lpane);
            } else {
                panes.add(lpane);
                panesPanel.add(lpane);
            }
        }

        if (backgroundPanes.size() != 0) {
            if (Debug.debugging("layerspanel")) {
                Debug.output("Adding BackgroundLayerSeparator");
            }
            panes.add(backgroundLayerSeparator);
            panesPanel.add(backgroundLayerSeparator);
            panes.addAll(backgroundPanes);

            Iterator it = backgroundPanes.iterator();
            while (it.hasNext()) {
                panesPanel.add((LayerPane) it.next());
            }

        } else if (backgroundLayerSeparator != null) {
            if (Debug.debugging("layerspanel")) {
                Debug
                        .output("No layers are background layers, adding separator");
            }
            panes.add(backgroundLayerSeparator);
            panesPanel.add(backgroundLayerSeparator);
        }

        setPanes(panes);

        if (scrollPane != null) {
            remove(scrollPane);
            scrollPane.removeAll();
            scrollPane = null;
        }

        scrollPane = new JScrollPane(panesPanel,
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setPreferredSize(new Dimension(50, 50));

        add(scrollPane, BorderLayout.CENTER);
        revalidate();
    }

}
